#!/usr/bin/python
# -*- coding: UTF-8 -*-

#######################################################
# 说明：SQL 存储过程代码格式化
# 作者：ChenRuKe
# 邮箱：413872434@qq.com
# 版本：1.0.23.08.12
# 用法：python SqlFormat.py "sql文件路径" 每次缩进空格个数默认为4
# 例子：python SqlFormat.py "d:\123.sql" 2
# 结果：会生成一个格式化好的SQL文件 d:\1234.sql.sql
#######################################################

from pickle import BINGET
import sys

def main():
    fn = ""
    spcc = 4
    if(len(sys.argv) > 1): 
        fn = sys.argv[1]
    if(len(sys.argv) > 2):
        spcc = int(sys.argv[2])
    if(fn != ""):    
        space = " " * spcc
        spci = 0

        bgn = "begin"
        nd = "end"
        bgni = 0

        #whl = "while"
        #iswhl = False
        f = "if"
        isf = False
        ls = "else"
        isls = False

        cmmntl ="--"
        cmmntlsb = "/*"
        cmmntlse = "*/"
        iscmmntls = False     

        st = "set"  
        
        frmtbdy = []
        with open(fn,'r', encoding='utf-8') as fo:
            for line in fo.readlines():
                lns = line.strip()
                ln = lns.lower()

                if(ln[-2:] == cmmntlse):
                    iscmmntls = False
                    frmtbdy.append(space * spci  + lns + '\n')
                    continue
                if(iscmmntls):
                    frmtbdy.append(space * spci  + lns + '\n')
                    continue;
                if(ln[:2] == cmmntl):
                    frmtbdy.append(space * spci  + lns + '\n')
                    continue
                if(ln[:2] == cmmntlsb):
                    iscmmntls = True
                    frmtbdy.append(space * spci  + lns + '\n')
                    if(ln[-2:] == cmmntlse):
                        iscmmntls = False
                    continue

                #if(ln[:3] == st):
                #    frmtbdy.append(space * spci  + lns + '\n')
                #    continue
                if(ln[:5] == bgn and ln[-3:] == nd):
                    if(isf or isls):
                        bgni-=1
                        spci-=1
                        isf = False
                        isls = False
                    frmtbdy.append(space * spci  + lns + '\n')
                    continue
                if(ln[:5] == bgn or ln[-5:] == bgn):
                    if(ln[:3] == nd):
                        bgni-=1
                        spci-=1
                    if(isf or isls):
                        bgni-=1
                        spci-=1
                        isf = False
                        isls = False
                    frmtbdy.append(space * spci  + lns + '\n')
                    bgni+=1
                    spci+=1
                    continue
                if(ln.find(bgn) > -1 and ln.find(nd) > -1):
                    frmtbdy.append(space * spci  + lns + '\n')
                    continue
                if(ln[:3] == nd):
                    bgni-=1
                    spci-=1                    
                    frmtbdy.append(space * spci  + lns + '\n')
                    if(ln[-4:] == ls):                        
                        isls = True
                        if(ln.find(f) > -1 and ln.find("(") > -1 and ln[-1:] == ")"):
                            isls = False
                            isf = True
                        spci+=1
                    continue
                if(ln[:2] == f):
                    isf = True
                    if(isls):
                        spci-=1
                    frmtbdy.append(space * spci  + lns + '\n')
                    spci+=1
                    if(ln.find(st) > -1 and ln.find(bgn) == -1):
                        spci-=1
                        isf = False
                    continue
                if(ln[:4] == ls):
                    isls = True
                    frmtbdy.append(space * spci  + lns + '\n')
                    if(ln.find(f) > -1 and ln.find("(") > -1 and ln[-1:] == ")"):
                        isls = False
                        isf = True
                    spci+=1
                    if(ln.find(st) > -1 and ln.find(bgn) == -1):
                        spci-=1
                        isls = False
                    continue
                if(isf or isls):
                    frmtbdy.append(space * spci  + lns + '\n')
                    isf = False
                    isls = False
                    spci-=1
                    continue
                else:
                    frmtbdy.append(space * spci  + lns + '\n')
                    
        with open(fn+".sql",'w', encoding='utf-8') as fo:
            fo.writelines(frmtbdy)
                                            
if __name__ == '__main__':
    main()
